package com.oodso.sell.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;
import com.oodso.sell.R;
import com.oodso.sell.utils.LogUtils;
import com.oodso.sell.utils.UiUtil;
import com.zhy.autolayout.utils.AutoUtils;

/**
 * 项目名称：Sosoliuda
 * 类描述：
 * 创建人：周作威
 * 创建时间：2016/11/16
 */

public class TabSpec extends RelativeLayout {
    private static final int UN_RES = -1;
    private int position;// 当前条目位置
    private TextView tabText = null;
    private Paint paint = null;
    private float radius;// 标记半径
    private float topPadding;// 显示标记顶部宽
    private boolean showReference;// 显示刷新
    private TextView badgerView;// 标记控件
    private ImageView tabSpecBg;

    public TabSpec(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setWillNotDraw(false);
        tabSpecBg = addTagImage(context);
        addTabView(context);
        addFlagView(context);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TabSpec);
        int specRes = typedArray.getResourceId(R.styleable.TabSpec_spec_res, UN_RES);
        if (UN_RES != specRes) {
            tabText.setCompoundDrawablesWithIntrinsicBounds(0, specRes, 0, 0);
        }
        int textRes = typedArray.getResourceId(R.styleable.TabSpec_spec_text, UN_RES);
        if (UN_RES != specRes) {
            tabText.setText(textRes);
        }
        // 整体背景,如果设置,则其他控件
        int bgRes = typedArray.getResourceId(R.styleable.TabSpec_spec_bg_res, UN_RES);
        if (UN_RES != bgRes) {
            tabText.setVisibility(View.GONE);
            tabSpecBg.setVisibility(View.VISIBLE);
            tabSpecBg.setImageResource(bgRes);
        }
        // 条目状态选择器
        int selectRes = typedArray.getResourceId(R.styleable.TabSpec_spec_item_select, -1);
        if (UN_RES != selectRes) {
            setBackgroundResource(selectRes);
        }
        radius = UiUtil.dip2px(context, 3);
        topPadding = UiUtil.dip2px(context, 5);
        showBadger(typedArray.getBoolean(R.styleable.TabSpec_spec_show_flag, false));
        // 设置是否选中状态
        boolean selected = typedArray.getBoolean(R.styleable.TabSpec_spec_select, false);
        setSelected(selected);
        position = typedArray.getInt(R.styleable.TabSpec_spec_position, UN_RES);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        typedArray.recycle();
    }

    @Override
    public void setSelected(boolean selected) {
        // TODO Auto-generated method stub
        super.setSelected(selected);
        if (selected) {
//            AnimationUtils.startViewImageAnim(tabText);
        }
    }

    /**
     * 获取imageview
     *
     * @return
     */
    public ImageView getImageView() {
        return tabSpecBg;
    }

    /**
     * 添加标记控件
     *
     * @param context
     */
    private void addFlagView(Context context) {
        badgerView = new TextView(context);
        int size = AutoUtils.getPercentWidthSize(32);
        LayoutParams params = new LayoutParams(size, size);
        params.addRule(ALIGN_PARENT_TOP | ALIGN_PARENT_RIGHT);
        params.rightMargin = (int) (size * 1.0);
        params.topMargin = AutoUtils.getPercentWidthSize(5);
        badgerView.setTextSize(11);
        badgerView.setText("0");
        badgerView.setTextColor(getResources().getColor(R.color.white));
        badgerView.setBackgroundResource(R.drawable.item_count);
        badgerView.setGravity(Gravity.CENTER);
        addView(badgerView, params);
    }

    private void addTabView(Context context) {
        tabText = new TextView(context);
        tabText.setGravity(Gravity.CENTER_HORIZONTAL);
        tabText.setCompoundDrawablePadding(UiUtil.dip2px(getContext(), 2));
        tabText.setTextColor(getResources().getColorStateList(R.color.bottom_text_selector));
        tabText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(tabText, params);
    }

    public void setCount(int i) {
        LogUtils.e("setCount", "" + i);
        badgerView.setVisibility(VISIBLE);
        if (i > 0 && i <= 99) {
            LogUtils.e("setCount", "if");
            badgerView.setText(i + "");
            ViewPropertyAnimator.animate(badgerView).alpha(1.0f).scaleX(1.2f).scaleY(1.2f).setDuration(300).setInterpolator(new LinearInterpolator());
        } else if (i > 99) {
            LogUtils.e("setCount", "else if");

            badgerView.setText("99");
        } else {
            LogUtils.e("setCount", "else");
            badgerView.setVisibility(GONE);
            ViewHelper.setAlpha(badgerView, 0f);
            ViewHelper.setScaleX(badgerView, 1.0f);
            ViewHelper.setScaleY(badgerView, 1.0f);
        }
    }

    /**
     * 获得整个tag Imageview
     *
     * @param context
     * @return
     */
    private ImageView addTagImage(Context context) {
        ImageView tabSpecBg = new ImageView(context);
        int padding = UiUtil.dip2px(context, 5);
        tabSpecBg.setPadding(padding, padding, padding, padding);
        tabSpecBg.setScaleType(ImageView.ScaleType.FIT_CENTER);
        addView(tabSpecBg, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
        return tabSpecBg;
    }

    public TabSpec(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabSpec(Context context) {
        this(context, null, 0);
    }

    public int getItemPosition() {
        return position;
    }

    public void setShowReference(boolean show) {
        showReference = show;
        invalidate();
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        super.setOnClickListener(l);
    }

    /**
     * 是否显示提示标志
     *
     * @param isShow
     */
    public void showBadger(final boolean isShow) {
        if (isShow) {
            ViewPropertyAnimator.animate(badgerView).alpha(1.0f).scaleX(1.2f).scaleY(1.2f).setDuration(300).setInterpolator(new LinearInterpolator());
        } else {
            ViewHelper.setAlpha(badgerView, 0f);
            ViewHelper.setScaleX(badgerView, 1.0f);
            ViewHelper.setScaleY(badgerView, 1.0f);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (showReference) {
            canvas.drawCircle(tabText.getRight(), topPadding, radius, paint);
        }
    }
}
